//
//  LDTRegistController.m
//  EPA
//
//  Created by lanou3g on 16/4/25.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "LDTRegistController.h"
#import "LDTNetWorkRequestManager.h"
#import "LDTLoginController.h"
@interface LDTRegistController ()
///邮箱
@property (weak, nonatomic) IBOutlet UITextField *emailTextFiled;
///昵称
@property (weak, nonatomic) IBOutlet UITextField *nicknameTextFiled;
///密码
@property (weak, nonatomic) IBOutlet UITextField *passWordTextFiled;



@end

@implementation LDTRegistController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;
    

}

#pragma mark - 注册点击事件
- (IBAction)didClickRegistBtn:(UIButton *)sender
{
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/user/reg"];
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    //设置POST
    [mRequest setHTTPMethod:@"POST"];
    //设置请求体
    NSString *boduStr = [NSString stringWithFormat:@"email=%@&gender=0&uname=%@&passwd=%@",self.emailTextFiled.text,self.nicknameTextFiled.text,self.passWordTextFiled.text];
    
    NSData *bodyData = [boduStr dataUsingEncoding:NSUTF8StringEncoding];
    [mRequest setHTTPBody:bodyData];
    
    //使用URLSession执行网络请求
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (data) {
            //  NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
            
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDcit = jsonDict[@"data"];
            
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:dataDcit[@"msg"]?dataDcit[@"msg"]:@"成功" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil]];
            
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self presentViewController:alertVC animated:YES completion:nil];
                
            });
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    }];
    //启动请求
    [dataTask resume];
}




#pragma mark - 返回按钮

- (void)leftButtonAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
